import Vue from 'vue'
import Vuex from 'vuex'

// install plug-in
Vue.use(Vuex);

// create object
const store = new Vuex.Store({
    // sharing variables
    state: {
        isLogin: false,  // user login status
    },
    // the only way to update variables in state of vuex through committing mutation which is a synchronized operation
    mutations: {
        userStatus(state, payload) {
            state.isLogin = payload.loginFlag
        },
        userStatusSessionStorage (state, payload) {  // 刷新时使用，sessionStorage会将所有的值转为字符串存储
            state.isLogin = payload.isLogin === 'true';
        }
    },
    // like mutation，but supporting asynchronous operation
    actions: {
        updateStateAsync(context, payload){
            context.commit("updateState", payload)
        }
    },
    // 基于state中的派生状态，可以理解为组件中的计算属性
    getters: {

    },
});

// export store object
export default store
